package com.yufa.springcloud.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yufa.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderConstroller {

    @Value("${user.url}")
    private String url;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EurekaClient client;

    @GetMapping(value = "/order/{id}")
    @HystrixCommand(fallbackMethod = "shibai")
    public User getOrder(@PathVariable Long id) {
        return restTemplate.getForObject(url + id, User.class);
    }

    private User shibai(Long id) {
        return new User().setId(-100L);
    }

    @GetMapping(value = "/order2/{id}")
    private User fromClient(@PathVariable Long id) {
        InstanceInfo nextServerFromEureka = client.getNextServerFromEureka("PROVIDER-USER", false);
        return restTemplate.getForObject(nextServerFromEureka.getHomePageUrl() + "/user/" + id, User.class);

    }

    @GetMapping(value = "/test/{id}")
    private User test(@PathVariable Long id) {

        return new User().setId(id);

    }
}
